package com.example.accountbook;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RadioGroup rgSelect;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private MainAccountFragment mainAccountFragment;
    private MainRecordFragment mainRecordFragment;
    private MainChartFragment mainChartFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加载布局
//        setContentView(R.layout.activity_login);

        mainAccountFragment = new MainAccountFragment();
        mainRecordFragment = new MainRecordFragment();
        mainChartFragment = new MainChartFragment();
        manager = getSupportFragmentManager();
        transaction= manager.beginTransaction();//启动事务

        /*依次添加Fragment对象到容器组件fy_main中*/
        transaction.add(R.id.fy_main,mainAccountFragment);
        transaction.add(R.id.fy_main,mainRecordFragment);
        transaction.add(R.id.fy_main,mainChartFragment);

        /*显示一个Fragment对象，隐藏其他2个*/
        transaction.show(mainAccountFragment);
        transaction.hide(mainRecordFragment);
        transaction.hide(mainChartFragment);
        transaction.commit();//提交事务

        rgSelect = findViewById(R.id.rg_select);
        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                transaction = manager.beginTransaction();
                switch (checkedId) {
                    case R.id.rb_account:
                        transaction.show(mainAccountFragment);
                        transaction.hide(mainRecordFragment);
                        transaction.hide(mainChartFragment);
                    break;

                    case R.id.rb_write:
                        transaction.hide(mainAccountFragment);
                        transaction.show(mainRecordFragment);
                        transaction.hide(mainChartFragment);
                        break;

                    case R.id.rb_chat:
                        transaction.hide(mainAccountFragment);
                        transaction.hide(mainRecordFragment);
                        transaction.show(mainChartFragment);
                        break;
                }
                transaction.commit();
            }
        });

//        TextView textView = findViewById(R.id.tv_register_login);
//        textView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
//                startActivity(intent);
//            }
//        });

    }
}